﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using EZCRS.HCMS.Data;
using EZCRS.HCMS.Entity;
using GFrame.Shadow;
using GFrame.Data;
using GFrame.Shadow.Search;

namespace EZCRS.HCMS.SqlClient
{
    public class sql_hcrs_ratecode
    { 
        /// <summary>
        /// 获取价格代码信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public HCRS_RateCode getRateCodeItem(int ID)
        {

            DataSet ds = ExecSql.GetDataSet("select * from HCRS_RateCode where id = " + ID.ToString());

            if (ds.Tables[0].Rows.Count > 0)

                return new HCRS_RateCode(ds.Tables[0].Rows[0]);
            else
                return null;
        }


        /// <summary>
        /// 判断计划名称是否存在
        /// </summary>
        /// <param name="strName"></param>
        /// <returns></returns>
        public bool IsExist(string strRateCode, int ID, int PID)
        {

            string strSQL = "select * from HCRS_RateCode where rate = '" + strRateCode + "' AND ID != " + ID.ToString() + " AND Property_id = " + PID.ToString();

            try
            {
                List<HCRS_RateCode> lst = ExecSql.GetTableList<HCRS_RateCode>(strSQL);

                if (lst.Count > 0)
                    return true;
                else
                    return false;
            }
            catch { return false; }

        }

        public bool insertorupdate(HCRS_RateCode rate)
        {
            return ExecSql.InsertOrUpdateData<HCRS_RateCode>(rate);
        }


        public List<HCRS_RATE_CODE_CHANNEL> GetChannelListByRateID(int RateID)
        {
            ShadowSearch<HCRS_RATE_CODE_CHANNEL> search = new ShadowSearch<HCRS_RATE_CODE_CHANNEL>();

            search.ConnectionName = MainBase.CONNECTION_STRING;

            return search.Search("Rate_ID = " + RateID.ToString());

        }

        public bool Save(HCRS_RateCode rate, List<Channel> ChannelList, Dictionary<string, bool> roomlist, int Language)
        {
            Shadow<HCRS_RateCode> shadow = new Shadow<HCRS_RateCode>(rate);
            shadow.ConnectionName = MainBase.CONNECTION_STRING;

            bool b = shadow.Save();

            if (b)
            {
                //save channel
                SQLWriter sw = new SQLWriter();

                sw.Begin(MainBase.CONNECTION_STRING);

                sw.RunCommandText("DELETE FROM HCRS_RATE_CODE_CHANNEL WHERE Rate_ID = " + rate.ID.ToString());

                foreach (Channel cl in ChannelList)
                {
                    sw.RunCommandText("INSERT INTO HCRS_RATE_CODE_CHANNEL (Rate_ID, Channel_ID) VALUES(" + rate.ID.ToString() + ", " + cl.ID.ToString() + ")");
                }
                
                if(sw.HasError){

                    sw.RollBack();

                    return false;
                }
                
                sw.Commit();

                //save rate plan
                sql_hcrs_Plan plan = new sql_hcrs_Plan();

                foreach (string key in roomlist.Keys)
                {
                    string strPlanCode = key + "-" + rate.RateCode;

                    if (!roomlist[key])
                    {
                        plan.setStatus(rate.Pid, strPlanCode, key, 0, Language);    //未选中则设置为未激活
                    }
                    else
                    {
                        //add or rewrite

                        HCRS_Plan obj = new HCRS_Plan();

                        obj.LanguageType = Language;
                        obj.Pid = rate.Pid;
                        obj.PlanCode = strPlanCode;
                        obj.RoomCode = key;
                        obj.PlanName = rate.Name;

                        plan.Add(obj);

                    }
                }
            }

            return b;
        }
    }
}
